package com.demo.easy.oneLine;

import com.alibaba.excel.EasyExcel;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @Author:YuXuePeng
 * @Date:Created in 2023/4/14 17:40
 */
@Service
public class TemplateServiceImpl implements TemplateService{
    @Override
    public List<TemplateEntity> importExcel(MultipartFile file) throws IOException {
        List<TemplateEntity> entities = getTemplateEntities(file);
        System.out.println(entities);
        return entities;
    }

    @Override
    public List<TemplateHeadlerEntity> importHeadlerExcel(MultipartFile file) throws IOException {
        List<TemplateHeadlerEntity> entities = getTemplateHeadlerEntities(file);
        System.out.println(entities);
        return entities;
    }
    private List<TemplateEntity> getTemplateEntities(MultipartFile file) throws IOException {
        TemplateListener listener = new TemplateListener();
        EasyExcel.read(file.getInputStream(), TemplateEntity.class,listener).sheet().doRead();

        return listener.getList();
    }
    private List<TemplateHeadlerEntity> getTemplateHeadlerEntities(MultipartFile file) throws IOException {
        TemplateHeadlerListener listener = new TemplateHeadlerListener();
        EasyExcel.read(file.getInputStream(), TemplateHeadlerEntity.class,listener).sheet(0).headRowNumber(2).doRead();

        return listener.getList();
    }
}
